<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
<!--      <el-form-item label="公司id" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="客户经理账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入客户经理账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人身份" prop="contactIdentity">
        <el-select v-model="queryParams.contactIdentity" placeholder="请选择联系人身份" clearable>
          <el-option
            v-for="dict in dict.type.customer_identity"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址/区域" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址/区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否含税" prop="taxIs">
        <el-select v-model="queryParams.taxIs" placeholder="请选择是否含税" clearable>
          <el-option
            v-for="dict in dict.type.tax_is"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态，是否成交" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dianlan:quotation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dianlan:quotation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dianlan:quotation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dianlan:quotation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="quotationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="公司id" align="center" prop="companyId" />-->
      <el-table-column label="客户经理账号" align="center" prop="userName" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="联系人姓名及电话" align="center" prop="contactPerson" />
      <el-table-column label="联系人身份" align="center" prop="contactIdentity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_identity" :value="scope.row.contactIdentity"/>
        </template>
      </el-table-column>
      <el-table-column label="地址/区域" align="center" prop="address" />
      <el-table-column label="图片" align="center" prop="file" />
<!--      <el-table-column label="其他说明" align="center" prop="remark" />-->
      <el-table-column label="是否含税" align="center" prop="taxIs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tax_is" :value="scope.row.taxIs"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="报价总金额" align="center" prop="amount" />
      <el-table-column label="报价人" align="center" prop="bidder" />
      <el-table-column label="报价次数" align="center" prop="frequency" />
      <el-table-column label="星级" align="center" prop="level" />
      <el-table-column label="原材料基数" align="center" prop="priceTon" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dianlan:quotation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dianlan:quotation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报价单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="公司id" prop="companyId">
          <el-select
            v-model="form.companyId"
            placeholder="请选择"
            size="large"
            style="width: 240px"
          >
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.companyName"
              :value="item.id"
            />
          </el-select>
<!--          <el-input v-model="form.companyId" placeholder="请输入公司id" />-->
        </el-form-item>
        <el-form-item label="客户经理账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入客户经理账号" />
        </el-form-item>
        <el-form-item label="联系人姓名及电话" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人姓名及电话" />
        </el-form-item>
        <el-form-item label="联系人身份" prop="contactIdentity">
          <el-select v-model="form.contactIdentity" placeholder="请选择联系人身份">
            <el-option
              v-for="dict in dict.type.customer_identity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址/区域" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址/区域" />
        </el-form-item>
        <el-form-item label="图片文件,原清单" prop="file">
          <image-upload v-model="form.file"/>
        </el-form-item>
        <el-form-item label="星级" prop="level">
          <el-input v-model="form.level" placeholder="请输入星级" />
        </el-form-item>
        <el-form-item label="其他说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否含税" prop="taxIs">
          <el-select v-model="form.taxIs" placeholder="请选择是否含税">
            <el-option
              v-for="dict in dict.type.tax_is"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态，是否成交" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态，是否成交">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报价总金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入报价总金额" />
        </el-form-item>
        <el-form-item label="报价人" prop="bidder">
          <el-input v-model="form.bidder" placeholder="请输入报价人" />
        </el-form-item>
        <el-form-item label="报价次数" prop="frequency">
          <el-input v-model="form.frequency" placeholder="请输入报价次数" />
        </el-form-item>
        <el-form-item label="原材料基数" prop="priceTon">
          <el-input v-model="form.priceTon" placeholder="请输入原材料基数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuotation, getQuotation, delQuotation, addQuotation, updateQuotation } from "@/api/dianlan/customer/quotation";
import {validUsername} from "@/utils/validate";
import {listCustomer} from "@/api/dianlan/customer/customer";

export default {
  name: "Quotation",
  dicts: ['customer_identity', 'order_status', 'tax_is'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报价单表格数据
      quotationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //个人客户列表
      customerList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        userName: null,
        contactPerson: null,
        contactIdentity: null,
        address: null,
        file: null,
        level: null,
        taxIs: null,
        status: null,
        amount: null,
        bidder: null,
        frequency: null,
        priceTon: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          { required: true, message: "公司id不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "客户经理账号不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址/区域不能为空", trigger: "blur" }
        ],
        taxIs: [
          { required: true, message: "是否含税不能为空", trigger: "change" }
        ],
        bidder: [
          { required: true, message: "报价人不能为空", trigger: "blur" }
        ],
        priceTon: [
          { required: true, message: "原材料基数不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.listCustomerByMy();
  },
  methods: {
    //获取个人客户列表
    listCustomerByMy(){
      const username = this.$store.state.user.name;
      listCustomer({ pageNum: 1, pageSize: 100,userName: username}).then(response => {
        this.customerList=response.rows;
      })
    },
    /** 查询报价单列表 */
    getList() {
      this.loading = true;
      listQuotation(this.queryParams).then(response => {
        this.quotationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        companyId: null,
        userName: this.$store.state.user.name,
        contactPerson: null,
        contactIdentity: null,
        address: null,
        file: null,
        level: null,
        remark: null,
        taxIs: null,
        status: null,
        amount: null,
        bidder: null,
        frequency: null,
        priceTon: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报价单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuotation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报价单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuotation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuotation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报价单编号为"' + ids + '"的数据项？').then(function() {
        return delQuotation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dianlan/quotation/export', {
        ...this.queryParams
      }, `quotation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
